import request from '@/utils/request';
import URLS from './APIUrlDefine';
import { CURRENT_TOKEN } from '@/components/Authorized/renderAuthorize';

export async function GetArticleList(data: any): Promise<any> {
  return request(`${URLS.GET_ARTICLE_LIST}/${data.current}/${data.pageSize}`, {
    method: 'GET',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
    },
  }).then((response) => {
    const data = response.data;
    const result = {
      data: data.records,
      total: data.total,
      success: true,
      pageSize: data.size,
      current: parseInt(`${data.current}`, 10) || 1,
    };
    return Promise.resolve(result);
  });
}

export async function CreateNewArticle(data: any): Promise<any> {
  return request(URLS.CREATE_NEW_ARTICLE, {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    data: JSON.stringify(data)
  });
}

export async function UpdateArticle(data: any): Promise<any> {
  return request(URLS.UPDATE_ARTICLE, {
    method: 'PUT',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    data: JSON.stringify(data)
  });
}

export async function ChangeArticleState(data: any): Promise<any> {
  return request(`${URLS.CHANGE_ARTICLE_STATE}/${data.id}/${data.state}`, {
    method: 'PUT',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    params: data
  });
}

export async function DeleteArticle(data: any): Promise<any> {
  return request(`${URLS.DELETE_ARTICLE}/${data.id}`, {
    method: 'DELETE',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    params: data
  });
}

export async function SearchGetArticle(keyword: string) {
  return request(`${URLS.SEARCH_ARTICLE}?key=${keyword}`, {
    method: 'GET',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
    },
  }).then((response) => {
    const data = response.data;
    return Promise.resolve(data);
  });
}

export async function GetTypeList(data: any): Promise<any> {
  return request(`${URLS.GET_ARTICLE_TYPE_LIST}/${data.current}/${data.pageSize}`, {
    method: 'GET',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
    },
  }).then((response) => {
    const data = response.data;
    const result = {
      data: data.records,
      total: data.total,
      success: true,
      pageSize: data.size,
      current: parseInt(`${data.current}`, 10) || 1,
    };
    return Promise.resolve(result);
  });
}

export async function ChangeTypeState(data: any): Promise<any> {
  return request(`${URLS.CHANGE_ARTICLE_TYPE_STATE}/${data.id}/${data.state}`, {
    method: 'PUT',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    params: data
  });
}

export async function CreateNewType(data: any): Promise<any> {
  return request(URLS.CREATE_NEW_ARTICLE_TYPE, {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    data: JSON.stringify(data)
  });
}

export async function UpdateType(data: any): Promise<any> {
  return request(URLS.UPDATE_ARTICLE_TYPE, {
    method: 'PUT',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    data: JSON.stringify(data)
  });
}

export async function DeleteType(data: any): Promise<any> {
  return request(`${URLS.DELETE_ARTICLE_TYPE}/${data.id}`, {
    method: 'DELETE',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    params: data
  });
}

export async function SearchGetTypeList(keyword: string) {
  return request(`${URLS.SEARCH_ARTICLE_TYPE}?key=${keyword}`, {
    method: 'GET',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
    },
  }).then((response) => {
    const data = response.data;
    return Promise.resolve(data);
  });
}

export async function GetTagList(data: any): Promise<any> {
  return request(`${URLS.GET_TAG_LIST}/${data.current}/${data.pageSize}?type=5`, {
    method: 'GET',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
    },
  }).then((response) => {
    const data = response.data;
    const result = {
      data: data.records,
      total: data.total,
      success: true,
      pageSize: data.size,
      current: parseInt(`${data.current}`, 10) || 1,
    };
    return Promise.resolve(result);
  });
}

export async function ChangeTagState(data: any): Promise<any> {
  return request(`${URLS.CHANGE_TAG_STATE}/${data.id}/${data.state}`, {
    method: 'PUT',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    params: data
  });
}

export async function CreateNewTag(data: any): Promise<any> {
  return request(URLS.CREATE_NEW_TAG, {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    data: JSON.stringify(data)
  });
}

export async function UpdateTag(data: any): Promise<any> {
  return request(URLS.UPDATE_TAG, {
    method: 'PUT',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    data: JSON.stringify(data)
  });
}

export async function DeleteTag(data: any): Promise<any> {
  return request(`${URLS.DELETE_TAG}/${data.id}`, {
    method: 'DELETE',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
      'content-type': 'application/json',
    },
    params: data
  });
}

export async function SearchGetTagList(keyword: string) {
  return request(`${URLS.SEARCH_TAG}?key=${keyword}&type=5`, {
    method: 'GET',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
    },
  }).then((response) => {
    const data = response.data;
    return Promise.resolve(data);
  });
}

export async function GetArticleDetailes(id: number) {
  return request(`${URLS.GET_ARTICLE_DETAILES}/${id}`, {
    method: 'GET',
    headers: {
      Authorization: `Bearer ${CURRENT_TOKEN}`,
    },
  }).then((response) => {
    const data = response.data;
    return Promise.resolve(data);
  });
}

